﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Utils;
namespace UI.ModSalary
{
    public partial class EmpItemAdd : System.Web.UI.Page
    {
        DefDicBiz defineDictionary = new DefDicBiz();
        SetsItemBiz setsItemBiz = new SetsItemBiz();
        EmpItemBiz empItemBiz = new EmpItemBiz();
        string empCode = "";
        string setsID = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Params["EmpCode"] != null)
            {
                empCode = Request.Params["EmpCode"].ToString();
                setsID = Request.Params["SetsID"].ToString();
            }
            if (setsID == "")
            {
                Response.Write("参数错误：缺少帐套ID");
                Response.End();
            }
            DataTable baseDt = empItemBiz.GetOneInfo(empCode);
            if (!IsPostBack)
            {
                EmployeeBiz employeeBiz = new EmployeeBiz();
                if (!string.IsNullOrWhiteSpace(empCode))
                {

                    DataTable dt = employeeBiz.GetOneInfo(empCode);
                    if (dt.Rows.Count > 0)
                    {
                        lbEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                        lbDeptName.Text = dt.Rows[0]["DeptName"].ToString();


                        string joinTime = dt.Rows[0]["JoinTime"].ToString();
                        if (Utils.RegexRule.IsDate(joinTime))
                            lbJoinTime.Text = Convert.ToDateTime(joinTime).ToString("yyyy-MM-dd");
                                               
                        lbState.Text = dt.Rows[0]["StateName"].ToString();
                        lbDutyName.Text = dt.Rows[0]["DutyName"].ToString();
                        lbEmpName.Text = dt.Rows[0]["EmpName"].ToString();

                        lbBankName.Text = dt.Rows[0]["SalaryState"].ToString();
                        lbBankAccount.Text = dt.Rows[0]["SalaryAccount"].ToString();
                    }
                    else
                    {
                        JSUtility.Alert(this, "不存在该员工编号: " + empCode);
                        return;
                    }
                }
                //获取员工基本设置
                int row = baseDt.Rows.Count;
                if (row > 0)
                {
                    defineDictionary.InitDropDownList("Wage_Gade", ddlSalGade, baseDt.Rows[0]["SalGade"].ToString().Trim());
                    // defineDictionary.InitSynDropDownList(ddlSalGade, baseDt.Rows[0]["SalGade"].ToString().Trim());
                    HourSalary.Text = baseDt.Rows[0]["HourSalary"].ToString();                   
                    btnAdd.Enabled = false;
                }
                else
                {
                    defineDictionary.InitDropDownList("Wage_Gade", ddlSalGade, "A");
                    btnUpdate.Enabled = false;
                    btnDelete.Enabled = false;
                }
            }
            FillBaseInfo(empCode, setsID, baseDt);
        }

        protected void FillBaseInfo(string empCode, string setsID,DataTable baseDt)
        {
            if (setsID == "" || setsID == "0")
                setsID = new EmpSetsBiz().GetSetsID(empCode);
            InitTextBox(empCode, setsID, baseDt);
        }
        //自动生成该帐套的项目
        private void InitTextBox(string empCode, string setsID, DataTable baseDt)
        {
            if (setsID != "")
            {
                Panel1.Controls.Add(new LiteralControl("<table style='width: 80%;' cellpadding=0 cellspacing=0 align=center class=td>"));
                Panel1.Controls.Add(new LiteralControl("\n"));
                DataTable dt = setsItemBiz.GetItemDict(setsID);

                //获取员工基本设置
                //DataTable baseDt= empItemBiz.GetOneInfo(empCode);


                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (i % 2 == 1)
                        Panel1.Controls.Add(new LiteralControl("<tr style='color:#4A3C8C;background-color:#E7E7FF;'>"));
                    else
                        Panel1.Controls.Add(new LiteralControl("<tr>"));
                    Panel1.Controls.Add(new LiteralControl(" <td style='width:75px;'>"));

                    TableCell cell = new TableCell();//创建单元格,也就是第一列
                    cell.Text = dt.Rows[i]["itemName"].ToString();//设置单元格内的文本
                    Panel1.Controls.Add(cell);
                    Panel1.Controls.Add(new LiteralControl("</td>"));
                    Panel1.Controls.Add(new LiteralControl(" <td >"));
                    TextBox textBox = new TextBox();

                    string initField = dt.Rows[i]["WageItem"].ToString();
                    textBox.ID = initField;
                    textBox.CssClass = "InputCss";
                    string initValue = dt.Rows[i]["InitValue"].ToString();
                    if (baseDt.Rows.Count > 0)
                    {
                        initValue = baseDt.Rows[0][initField].ToString();
                    }
                   
                    if (initValue != "0.00") textBox.Text = initValue;
                    Panel1.Controls.Add(textBox);

                    Panel1.Controls.Add(new LiteralControl("</td>"));

                    Panel1.Controls.Add(new LiteralControl("</tr>"));
                    Panel1.Controls.Add(new LiteralControl("\n"));
                }

                Panel1.Controls.Add(new LiteralControl("</table>"));
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {

            try
            {
                if (string.IsNullOrWhiteSpace(empCode))
                {
                    Utils.JSUtility.Alert(this, "工号不能为空");
                    return;
                }
                if (!new EmployeeBiz().IsExistEmpCode(empCode))
                {
                    Utils.JSUtility.Alert(this, "该工号没有员工使用");
                    return;
                }
                if (empItemBiz.IsExistEmpCode(empCode))
                {
                    Utils.JSUtility.Alert(this, "只可以更新");
                    return;
                }
                //判断有没有工资帐套，没有则加入
                EmpSetsBiz empSetsBiz = new EmpSetsBiz();
                if (!empSetsBiz.IsExistEmpCode(empCode))
                    empSetsBiz.ConfigSets(empCode, "", setsID);
                //加入个人工资项
                SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
                sdList.Add("EmpCode", empCode);
                sdList.Add("SalGade", ddlSalGade.SelectedItem.Text);             

                foreach (System.Web.UI.Control control in this.Form.Controls)
                {
                    FindSelecedControl(control, sdList);
                }
              
                if (empItemBiz.Insert("Wage_EmpItem", sdList))
                    JSUtility.Alert(this, "增加员工基本工资信息成功");
                else
                    JSUtility.Alert(this, "增加员工基本工资信息失败");

            }
            catch (Exception ee)
            {
                LogHelper.WriteLog(ee.Message);
                JSUtility.Alert(this, "增加员工基本工资信息失败");
            }


        }
        private void FindSelecedControl(Control control, SortedDictionary<string, string> sdList)
        {
            if (control is TextBox)
            {
                TextBox txt = (TextBox)control;
                string value = txt.Text.Trim();
                if (!string.IsNullOrWhiteSpace(value))
                    sdList.Add((control as TextBox).ID, value);

            }
            else
            {
                for (int i = 0; i < control.Controls.Count; i++)
                {
                    FindSelecedControl(control.Controls[i], sdList);
                }
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(empCode))
            {
                Utils.JSUtility.Alert(this, "工号不能为空");
                return;
            }
            if (!empItemBiz.IsExistEmpCode(empCode))
            {
                Utils.JSUtility.Alert(this, "只可以增加");
                return;
            }

            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("EmpCode", empCode);
            sdList.Add("SalGade", ddlSalGade.SelectedItem.Text);


            foreach (System.Web.UI.Control control in this.Form.Controls)
            {
                FindSelecedControl(control, sdList);
            }
           
           
            if (empItemBiz.Update("Wage_EmpItem", sdList))
                JSUtility.Alert(this, "修改员工基本工资信息成功");
            else
                JSUtility.Alert(this, "修改员工基本工资信息失败");

        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (empItemBiz.DeleteOneInfo(empCode))
            { JSUtility.Alert(this, "删除信息成功"); }
            else
                JSUtility.Alert(this, "删除信息失败");
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            JSUtility.CloseWindow();
        }
    }
}